﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Media;
using System.Windows.Forms;






namespace InfraTools
{
    public partial class FrmInfraTools : Form
    {
        //Settings Vars  
        public string glpiWebServiceUrl = Properties.Settings.Default.GlpiWebServiceUrl;
        public int GlpiDefaultEntity = Properties.Settings.Default.GlpiDefaultEntity;
       
        //Create Connectors 
        public Glpi Glpicon = new Glpi();
        public Sccm Sccmcon = new Sccm();
        
        public Class.Infratoolbox Toolbox = new Class.Infratoolbox();

        
            
        //set Vars
        public String SccmDefaultCollection = Properties.Settings.Default.SccmDefaultCol;
        public String SccmUserName = Properties.Settings.Default.SccmUsr;
        public String SccmPwd = Properties.Settings.Default.SccmPwd;
        public String SccmSitecode = Properties.Settings.Default.SccmSitecode;
        public String SccmServer = Properties.Settings.Default.SccmServer;
        public int GlpiBareMetalStatus = Properties.Settings.Default.GlpiBareMetalStatus;
        public int GlpiStagedStatus = Properties.Settings.Default.GlpiStagedStatus;
        public int GlpInUseStatus = Properties.Settings.Default.GlpInUseStatus;

        public Computers TempPc = new Computers();
        
        public FrmInfraTools()
        {
            
            InitializeComponent();
            
  
        }

        private void FrmInfraTools_Load(object sender, EventArgs e)
        {

            
            
            

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            this.Close();
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmSettings SettingsForm = new FrmSettings();
            if (Glpicon.isConnected(false))
            {
                SettingsForm.glpicon = Glpicon;
            }
            SettingsForm.Show();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Forms.FrmAbout AboutForm = new Forms.FrmAbout();
            AboutForm.Show();
        }

        private void buttonGo_Click(object sender, EventArgs e)
        {

            try 
	        {
                if (Glpicon.isConnected(true))
                {

                    SystemSounds.Beep.Play();
                    setlayout(3);
                 
                }

                
	        }
	        catch (Exception)
	        {
		    	throw;
	        }
            

            
           
        }

        private void cmbactions_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (true)
            {

                switch (int.Parse(cmbActions.SelectedValue.ToString()))
                {
                    case 1:
                        //add 2 Sccm
                        setlayout(2);
                        break;
                    case 2:
                        //Add2Inventory
                        setlayout(4);

                        break;
                    case 3:
                        //recycle
                        setlayout(2);
                        //MessageBox.Show("choice3");
                        break;
                    case 4:
                        //Remove - Free from inventory
                        setlayout(2);
                        // MessageBox.Show("choice4");
                        break;
                    case 5:
                        //Recycle
                        setlayout(2);
                        // MessageBox.Show("choice4");
                        break;
                    default:
                        setlayout(2);
                        //do default
                        break;
                }

            }
           
        }
        
        private void TxtBxGlpiPass_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                
                btGlpiAction.PerformClick();

            }
        }

        private void btGlpiAction_Click(object sender, EventArgs e)
        {
            Glpicon.restWsUrl= glpiWebServiceUrl;
            Glpicon.entity = GlpiDefaultEntity;
            Glpicon.glpiUsr = TxtBxGlpiUsr.Text;
            Glpicon.glpiPass= TxtBxGlpiPass.Text;


            if (!Glpicon.isConnected(false) & !string.IsNullOrWhiteSpace(TxtBxGlpiUsr.Text) & !string.IsNullOrWhiteSpace(TxtBxGlpiPass.Text))
                {
                    // Waiting  hour glass 

                    Cursor.Current = Cursors.WaitCursor;



                        btGlpiAction.Text = "Log In";

                        Glpiuser lgusr = Glpicon.doLogin();
                        if (Glpicon.isConnected(false))
                        {
                          
                            toolStripGlpiStatusLbl.Text = "Logged with: " + lgusr.realname + " " + lgusr.firstname ;
                            btGlpiAction.Text = "Log Out";
                            getfields();
                            Toolbox.UpdateFPCL(Glpicon, Sccmcon, "SMS00001");




                        }


                }

                else if (Glpicon.isConnected(false))
                {
 
                        btGlpiAction.Text = "Log Out";
                        
                        if (Glpicon.doLogout())
                        {   
                            
                            TxtBxGlpiUsr.Text = null;
                            TxtBxGlpiPass.Text = null;
                            btGlpiAction.Text = "Log In";
                            toolStripGlpiStatusLbl.Text = "Not Connected";
                        }


                }

            // Back to normal 

            Cursor.Current = Cursors.Default; 
    

        
           

           
                
        }

        //actions
        private void getfields()
        {
            if (Glpicon.isConnected(false))
            {
                

                
                cmbGlpiStatus.DataSource = Glpicon.getCmbValues("state");
                cmbGlpiItemType.DataSource = Toolbox.Objecttypes;
                cmbActions.DataSource = Toolbox.Choises;
                cmbPcTypes.DataSource = Toolbox.PCtypes;

                cmbSccmCol.DataSource = Sccmcon.GetCollections();

                Toolbox.UpdateFPCL(Glpicon, Sccmcon, Properties.Settings.Default.SccmFilterCol);
            }
        
        }
    

        private void cmbPcTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
           //label1.Text = cmbPcTypes.SelectedIndex.ToString();
        }

     

        private void txtBarcode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter && Glpicon.isConnected(true))
            {
                 

                switch (int.Parse(cmbActions.SelectedValue.ToString()))
                {
                    case 1:

                        MessageBox.Show(cmbActions.SelectedText);
                        break;


                    case 2:

                        //MessageBox.Show("Add2inventory");
                        setlayout(1);
                        
                        if (cmbGlpiItemType.Text == "Computer")
                        {
                           Class.BarCode BC = new Class.BarCode(cmbGlpiItemType.SelectedValue.ToString(), Toolbox.getfreepcname(cmbPcTypes.SelectedIndex));

                                if (Glpicon.GetComputerBySerial(BC.Serial) != null)
                                {
                                    BC.GetCNewComputer(txtBarcode.Text);
                                    

                                }

                                if (BC.PC.serial != null)
                                {
                                    MessageBox.Show(BC.PC.serial);
                                }    
                            
                        }
                        //else if (cmbGlpiItemType.Text == "Monitor")
                        //{

                        //    if (!string.IsNullOrEmpty(Glpicon.GetMonitorBySerial(BC.Serial).id))
                        //    {
                        //        //MessageBox.Show("Monitor Doesn't Exist");
                        //        MessageBox.Show("");

                        //        //Glpicon.GetMonitorBySerial(BC.Serial);
                        //    }
                        //}
                        

                        ////MessageBox.Show(BC.type + " " + BC.uuid + " " + BC.MAC + " " + BC.Serial);
                        
                        break;


                    case 3:
                        MessageBox.Show(cmbActions.SelectedText);

                        break;
                    case 4:
                        MessageBox.Show(cmbActions.SelectedText);
                        
                        break;

                    default:
                        //do default
                        break;
                }
                txtBarcode.SelectAll();


                
            }


        }

   

private void setlayout(int isInventory)
{

    //MessageBox.Show(isInventory.ToString());
    switch (isInventory)
    {
        case 1 :
               
        
              pnlInventory.Show();
              cmbActions.Enabled = false;
              cmbGlpiItemType.Enabled = false;

              
              btnCancell.Show();
              break;
                
        case 2 :
              //add 2sccm -- default
              cmbSccmCol.SelectedItem = SccmDefaultCollection;
              cmbActions.Enabled = true;
              cmbGlpiItemType.Enabled = true;
              btnCancell.Hide();
              pnlSccm.Show();
              pnlInventory.Hide();
              break;
        case 3 :

              computersBindingSource.Clear();
              cmbActions.Enabled = true;
              cmbGlpiItemType.Enabled = true;

              break;

        case 4 :
              //Add 2 Inventory 
              pnlSccm.Hide();
              pnlInventory.Show();
              cmbGlpiStatus.SelectedValue = GlpiBareMetalStatus;
              
              break;
            
        default:
              
              break;
    }



}

private void btnCancell_Click(object sender, EventArgs e)
{
    setlayout(3);
}

private void helpToolStripButton_Click(object sender, EventArgs e)
{
    
  // MessageBox.Show(Toolbox.getfreepcname(cmbPcTypes.SelectedIndex));
    MessageBox.Show(Properties.Settings.Default.GlpInUseStatus.ToString());
}

private void copyToolStripButton_Click(object sender, EventArgs e)
{
    
    Toolbox.UpdateFPCL(Glpicon, Sccmcon, "SMS00001");
}

private void computersBindingSource_CurrentChanged(object sender, EventArgs e)
{

}

private void printToolStripButton_Click(object sender, EventArgs e)
{

}

private void saveToolStripButton_Click(object sender, EventArgs e)
{

}

private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
FrmSettings settingsfrm = new FrmSettings() ;
settingsfrm.glpicon = Glpicon;
settingsfrm.Show();
}

private void restartWdsToolStripMenuItem_Click(object sender, EventArgs e)
{
    Sccmcon.RestartWDS();
}



























      



     }
}
